package com.listener;

import com.pojo.Auth;
import com.service.AuthService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.ServletContextAware;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 监听器
 *
 * @author lxw
 * @create 2017-08-17 15:06
 **/
@Controller
public class SystemListener  implements InitializingBean,ServletContextAware {
    private static Logger logger = Logger.getLogger(SystemListener.class);

    @Resource
    private AuthService authService;

    public void  afterPropertiesSet() throws Exception{}
    public void setServletContext(ServletContext servletContext){
        //获取权限表中所有的url--list存储的是一个对象集合
        List<Auth> list= authService.allUrls();
        int i = 0;
       /* List<String> list2 = new ArrayList<String>();
        for(Auth auth :list)
        {
            list2.add(auth.getUrl());
        }
        String[] arr=new String[list.size()];
        //将对象中的url存到数组中
        list2.toArray(arr);*/
        Map<Integer, String> map = new HashMap<Integer, String>();
        for (Auth auth : list) {
            map.put(i++, auth.getUrl());
        }

        //存入所有的url到session中
        servletContext.setAttribute("map",map);
        logger.info("-------------初始化数据成功----------");
    }
}